package com.whalecloud.chart.controller;

import com.whalecloud.chart.domain.DrawChartRequestDTO;
import com.whalecloud.chart.service.DrawChartFactory;
import com.whalecloud.chart.util.FileUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.File;

/**
 * @Description
 * @Author MarkAsh
 * @Date 2025/2/22
 **/
@RestController
@RequestMapping
public class AgentDrawChartController {


    /**
     * 根据源数据生成图表
     * @param requestDTO 请求参数
     * @param response 响应
     */
    @PostMapping("v1/api/draw-chart")
    public void drawChartFromSourceData(@RequestBody DrawChartRequestDTO requestDTO, HttpServletResponse response) {
        // 生成图表
        File file = DrawChartFactory.drawChart(requestDTO);
        // 写出文件
        FileUtils.writeFile(file, response);
    }

    /**
     * 获取图表示例数据
     * @param chartType 图表类型
     * @return 示例数据
     */
    @GetMapping("v1/api/chart-sample-data")
    public DrawChartRequestDTO getChartSampleData(@RequestParam String chartType){
        return DrawChartFactory.getSampleData(chartType);
    }
}
